/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.server.rpc;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.daikit.commons.shared.bean.AbstractDkBeanWithId;
import com.daikit.commons.shared.exception.DkException;
import com.daikit.commons.shared.exception.authentication.DkNotEnoughPermissionException;
import com.daikit.commons.shared.exception.authentication.DkUserNotAuthenticatedException;
import com.daikit.daikit4gxtsample.server.scope.SessionParametersContainer;
import com.daikit.daikit4gxtsample.server.service.SessionService;
import com.daikit.daikit4gxtsample.shared.bean.User;


/**
 * Base RPC Implementation that all RPC implementations should extend.
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public abstract class AbstractBaseRpcImpl extends com.daikit.daikit4gxt.server.rpc.AbstractDkBaseRpcImpl
{

	protected final Log log = LogFactory.getLog(getClass());

	@Autowired
	protected SessionService sessionService;
	@Autowired
	protected SessionParametersContainer sessionParametersContainer;

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// OVERRIDEN METHODS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	@Override
	protected String getLanguageIsoCode()
	{
		return sessionParametersContainer.getCurrentLocale() == null ? null : sessionParametersContainer.getCurrentLocale()
				.getISO3Language();
	}

	@Override
	protected String getUserLoggedName()
	{
		return AbstractDkBeanWithId.asSimpleString(sessionParametersContainer.getLoggedUser());
	}

	@Override
	protected void processApplicationSpecificException(final Exception e) throws DkException
	{
		// Delegated to default exception processor in parent class
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// METHODS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	protected User assertIsLogged() throws DkException
	{
		final User user = sessionService.getHttpSessionUser();
		if (user == null)
		{
			throw new DkUserNotAuthenticatedException();
		}
		return user;
	}

	protected void assertIsSuperAdmin() throws DkException
	{
		final boolean isSuperAdmin = sessionService.getHttpSessionUser() != null
				&& "admin".equals(sessionService.getHttpSessionUser().getEmail());
		if (!isSuperAdmin)
		{
			throw new DkNotEnoughPermissionException();
		}
	}

	protected User assertIsLoggedAndSuperAdmin() throws RuntimeException
	{
		final User user = assertIsLogged();
		assertIsSuperAdmin();
		return user;
	}

}
